﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Realtors.Entity;

namespace Realtors.Repository
{
    public class HouseRepositoryDB : DbContext, IHouseRepository
    {
        public DbSet<House> Houses { get; set; }



        IQueryable<House> IHouseRepository.GetActiveHouses()
        {
            return Houses.Where(c => c.Deleted == false).Where(c => c.Active == true) ;
        }

        House IHouseRepository.GetHouseByID(Guid houseID)
        {
            return Houses.Where(c => c.Deleted == false).SingleOrDefault(c => c.EntityId == houseID);
        }

        void IHouseRepository.InsertHouse(House house)
        {
            throw new NotImplementedException();
        }

        void IHouseRepository.DeleteHouse(Guid houseID)
        {
            var house2Delete = Houses.Where(c => c.Deleted == false).SingleOrDefault(c => c.EntityId == houseID);
            if (house2Delete != null)
            {
                house2Delete.Deleted = true;
                house2Delete.DateDeleted = DateTime.UtcNow;
                this.SaveChanges();
            }

        }

        void IHouseRepository.UpdateHouse(House house)
        {
            throw new NotImplementedException();
        }

        void IHouseRepository.Save()
        {
            this.SaveChanges();
        }

        void IDisposable.Dispose()
        {
            base.Dispose();
        }


        IQueryable<House> IHouseRepository.GetActiveFeaturedHouses()
        {
            return Houses.Where(c => c.Deleted == false).Where(c => c.Active == true).Where(c => c.Featured == true);
        }
    }
}
